package toy.keli.edic.ui.home.homescreen

import android.annotation.SuppressLint
import android.widget.Toast
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import com.google.accompanist.glide.rememberGlidePainter
import toy.keli.edic.ui.home.HomeEvents
import toy.keli.edic.ui.home.WordModelViewModel
import toy.keli.edic.ui.home.WordState
import toy.keli.edic.ui.home.components.CopyButton
import toy.keli.edic.ui.home.components.SaveButton
import toy.keli.edic.ui.home.components.SignButton
import toy.keli.edic.ui.home.components.UtilButtons
import toy.keli.edic.ui.main.components.Display
import toy.keli.edic.ui.main.components.Display.mt
import toy.keli.edic.ui.main.components.Display.px2dp
import toy.keli.edic.ui.main.components.SelectButton
import toy.keli.edic.util.ActivityRun


/**
 * 首页视图 实现底部功能按钮和背景
 */
@SuppressLint("StateFlowValueCalledInComposition")
@Composable
@Preview
fun SearchComponent(
    wordState: WordState?=null,
    wordViewModel: WordModelViewModel?=null,
    nextDo: (t:Int)->Unit={}
) {
    if( LocalInspectionMode.current) {
        ActivityRun.context = LocalContext.current

    }
    val vScsroll = rememberScrollState()
    HomeEvents.status.openDrowUp = vScsroll.value.toInt()==0
    HomeEvents.status.enableDrowDown = vScsroll.value == vScsroll.maxValue

    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 2.dp, horizontal = 32.dp)
            .verticalScroll(vScsroll),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        Card(
            shape = RoundedCornerShape(8.dp),
            //backgroundColor = MaterialTheme.colorScheme.surface,
            modifier = Modifier
                .fillMaxWidth()
        ) {

            Box {
                var pic = ""
                if(wordState!=null && wordState.wordModel!=null && wordState.wordModel?.hasPic()!!) {
                    pic = wordState.wordModel.pic
//                    val iSize = remember {
//                        val ds = Display.getScreenSize()
//                        var w= px2dp((ds.x*0.75).toInt())
//                        var h= px2dp((ds.y*0.75).toInt())
//                        if (ds.x > ds.y) {
//                            w = px2dp(ds.x)
//                        } else {
//                            h=px2dp((ds.y * 0.618).toInt())
//                        }
//                        DpOffset(w,h)
//                    }
//                    Image(
//                        painter = rememberGlidePainter(request = wordState.wordModel.pic),
//                        contentDescription = "pic",
//                        modifier = Modifier
//                            .alpha(0.4f)
//                            .size(iSize.x,iSize.y)
//                            .align(Alignment.TopCenter)
//                    )
                }

                Column {
//                    UtilButtons(wordViewModel)
                    DefaultPageWordDetail(wordState?.wordModel,pic=pic)
                }
            }

        }

        AnimatedVisibility(visible = true) {
            val ct = LocalContext.current
            val wordModel = wordViewModel?.wordState?.value?.wordModel
            Row (modifier = Modifier){
                SignButton(wordModel?.status ?: 0)
                Spacer(Modifier.weight(1f))
                CopyButton(wordState?.wordModel?.word ?: "")
                Spacer(Modifier.weight(1f))
                SaveButton(){
                    if(wordModel!=null) {
                        wordViewModel?.insertBookmark(wordModel)
//                        Toast.makeText(ct, mt("Saved"), Toast.LENGTH_SHORT).show()
                    }
                }
            }

        }
    }
}